import {defineStore} from 'pinia'
import {ref} from 'vue'
import {getLogin} from '@/api/Login/index'
import {useCartStore} from './useCartStore'
import {mergeCart} from '@/api/cart'
export const useLoginStore = defineStore('userInfo',()=>{
    const userInfo = ref({});
    const status = ref(500)
    const cartStore = useCartStore()
    const getUser = async (user:Object)=>{
        const res = await getLogin(user)
        userInfo.value = res.data.result
        status.value = res.status
        //合并用户购物车与本地购物车列表
        await mergeCart(cartStore.list.map(item=>{
            return {
                skuId:item.skuId,
                selected:item.selected,
                count:item.count
            }
        }))
        //更新购物车列表
        cartStore.updateCartList()
    }
    //退出登录
    const userOut = ()=>{
        userInfo.value = {}
        cartStore.clearCart()
    }
    return {
        userInfo,
        status,
        getUser,
        userOut
    }
},{
    persist:{
        paths:['userInfo']
    }
})